import javax.swing.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: a1227
 * Date: 2022-07-22
 * Time: 16:43
 */
class ListNode{
    public int val;//数据
    public ListNode next;//存放下一个节点
    //构造方法
    public ListNode(int val){
        this.val = val;
    }
}

public class MySingleLinkedList {
    public ListNode head;//头节点
    //头插法
    public void addFirst(int data){
        ListNode newNode = new ListNode(data);//头插的新节点
        newNode.next = head;
        head = newNode;
    }
    //打印单链表
    public void display(){
        //单链表为空的情况
        if(head == null){
            System.out.println("单链表为空，无法打印！");
            return ;
        }
        //单链表不为空的情况
        ListNode cur = this.head;
        while(cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }
    //尾插法
    public void addLast(int data){
        //head为空的情况
        if(this.head == null){
            addFirst(data);//head为空，调用头插插入元素
            return ;
        }
        //head不为空的情况
        ListNode cur = this.head;
        while(cur.next != null){
            cur = cur.next;
        }
        ListNode newNode = new ListNode(data);//尾插的新节点
        cur.next = newNode;
    }
    //找index的前驱prev
    public ListNode findPrev(int index){
        int prevIndex = index - 1;//根据index的数值，需要找index-1次
        ListNode prev = this.head;
        //找的过程
        while(prevIndex-- != 0){
            prev = prev.next;
        }
        return prev;
    }
    //得到单链表的长度
    public int size(){
        ListNode cur = this.head;
        int count = 0;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }
    //在下标为index处插入data
    public void addIndex(int index,int data){
        //index值不合法的情况
        if(index < 0 || index > size()){
            System.out.println("index位置不合法！");
            return ;
        }
        //index为0的情况
        if(index == 0){
            addFirst(data);//调用头插
            return ;
        }
        //合法的情况
        ListNode prev = findPrev(index);
        ListNode newNode = new ListNode(data);
        newNode.next = prev.next;
        prev.next = newNode;
    }
    //查找关键字key是否在单链表当中
    public boolean contains(int key){
        //单链表为空的情况
        if(this.head == null){
            System.out.print("单链表为空：");
            return false;
        }
        //单链表不为空的情况
        ListNode cur = this.head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        //单链表为空的情况
        if(this.head == null){
            System.out.println("单链表为空,无法删除！");
            return ;
        }
        ListNode cur = this.head;
        ListNode headCopy = this.head; //保留head的原初数据
        while(cur.next != null && cur.next.val != key){
            cur = cur.next;
        }
        //除头节点外，其他节点可能存在key的情况
        if(cur.next != null) {
            cur.next = cur.next.next;
        }
        //其他节点都不存在key，头节点可能存在key的情况
        if(cur.next == null){
            if(this.head.val == key){
                this.head = this.head.next;
            }
        }
        //所有节点都不存在key的情况
        if(this.head == headCopy){
            System.out.println("找不到所要删除的数字");
        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        //单链表为空的情况
        if(this.head == null){
            System.out.println("单链表为空，无法删除！");
            return ;
        }
        ListNode cur = this.head.next;
        ListNode prev = this.head;
        while(cur != null){
            //cur的val等于key的情况
            if(cur.val == key){
                cur = cur.next;
                prev.next = cur;
            }else{               //cur的val不等于key的情况
                prev = cur;
                cur = cur.next;
            }
        }
        //判断头节点的val是否等于key
        if(this.head.val == key){
            this.head = this.head.next;
        }
    }
    //清空单链表
    public void clear(){
        //暴力删法
        this.head = null;
        //温柔删法
        ListNode cur = this.head;
        while(cur != null){
            cur = cur.next;
            this.head = null;
            this.head = cur;
        }
    }
}
